package Views;

import Controllers.RentalCTR;
import Models.*;
import Views.TableModels.RentalTableModel;
import byggemarked.Tools;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

/**
 * @author Erik og Morten
 */
public class RentalView extends javax.swing.JFrame implements WindowListener
{
	private Customer chosenCustomer;
	private Machine chosenMachine;
	private MachinePicker mp;
	private CustomerPicker cp;
	private RentalCTR controller = new RentalCTR();

    public RentalView()
	{
        initComponents();
		findTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(findTable.getSelectedRowCount() == 1)
					{
						int chosenId = Integer.parseInt(findTable.getValueAt(findTable.getSelectedRow(), 0).toString());
						new RentalInfoView(controller.getRental(chosenId)).setVisible(true);
					}
				}
			}
		 });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        RentalTP = new javax.swing.JTabbedPane();
        addRentalPanel = new javax.swing.JPanel();
        addDescLabel = new javax.swing.JLabel();
        addCustomerLabel = new javax.swing.JLabel();
        addMachineLabel = new javax.swing.JLabel();
        addDepositLabel = new javax.swing.JLabel();
        addPrisPerDayLabel = new javax.swing.JLabel();
        addCustomerTF = new javax.swing.JTextField();
        addMachineTF = new javax.swing.JTextField();
        addDepositSp = new javax.swing.JSpinner();
        addPricePerDaySp = new javax.swing.JSpinner();
        addCustomerFindButton = new javax.swing.JButton();
        addMachineFindButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        findRentalPanel = new javax.swing.JPanel();
        findDescLabel = new javax.swing.JLabel();
        findRentalLabel = new javax.swing.JLabel();
        findExpectedDateLabel = new javax.swing.JLabel();
        findReturnedDateLabel = new javax.swing.JLabel();
        findCustomerIdLabel = new javax.swing.JLabel();
        findMachineIdLabel = new javax.swing.JLabel();
        findDepositLabel = new javax.swing.JLabel();
        findPricePerDayLabel = new javax.swing.JLabel();
        findTotalPriceLabel = new javax.swing.JLabel();
        findRentalTF = new javax.swing.JTextField();
        findExpectedDateTF = new javax.swing.JTextField();
        findReturnedDateTF = new javax.swing.JTextField();
        findCustomerTF = new javax.swing.JTextField();
        findMachineTF = new javax.swing.JTextField();
        findDepositSP = new javax.swing.JSpinner();
        findPricePerDaySP = new javax.swing.JSpinner();
        findTotalPriceSP = new javax.swing.JSpinner();
        findSearchButton = new javax.swing.JButton();
        findCustomerFindButton = new javax.swing.JButton();
        findMachineFindButton = new javax.swing.JButton();
        findScrollPane = new javax.swing.JScrollPane();
        findTable = new javax.swing.JTable();
        findShowAllButton = new javax.swing.JButton();
        getRentalPanel = new javax.swing.JPanel();
        getDescLabel = new javax.swing.JLabel();
        getIdLabel = new javax.swing.JLabel();
        getIdSp = new javax.swing.JSpinner();
        getSearchButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Udlejning");

        addRentalPanel.setPreferredSize(new java.awt.Dimension(0, 0));

        addDescLabel.setText("Her kan der tilføjes udlån");

        addCustomerLabel.setText("Kunde:");

        addMachineLabel.setText("Maskine:");

        addDepositLabel.setText("Depositum:");

        addPrisPerDayLabel.setText("Pris per dag:");

        addCustomerTF.setEnabled(false);

        addMachineTF.setEnabled(false);

        addDepositSp.setValue(500);

        addPricePerDaySp.setValue(50);

        addCustomerFindButton.setText("Find");
        addCustomerFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCustomerFindButtonActionPerformed(evt);
            }
        });

        addMachineFindButton.setText("Find");
        addMachineFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addMachineFindButtonActionPerformed(evt);
            }
        });

        addButton.setText("Tilføj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout addRentalPanelLayout = new org.jdesktop.layout.GroupLayout(addRentalPanel);
        addRentalPanel.setLayout(addRentalPanelLayout);
        addRentalPanelLayout.setHorizontalGroup(
            addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addDescLabel)
                .add(258, 258, 258))
            .add(addRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addCustomerLabel)
                    .add(addMachineLabel)
                    .add(addDepositLabel)
                    .add(addPrisPerDayLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addCustomerTF)
                    .add(addMachineTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                    .add(addPricePerDaySp)
                    .add(addDepositSp, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addMachineFindButton)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addCustomerFindButton))
                .addContainerGap(117, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, addRentalPanelLayout.createSequentialGroup()
                .addContainerGap(344, Short.MAX_VALUE)
                .add(addButton)
                .addContainerGap())
        );
        addRentalPanelLayout.setVerticalGroup(
            addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addDescLabel)
                .add(18, 18, 18)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addCustomerLabel)
                    .add(addCustomerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addCustomerFindButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addMachineLabel)
                    .add(addMachineTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addMachineFindButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addDepositLabel)
                    .add(addDepositSp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addPrisPerDayLabel)
                    .add(addPricePerDaySp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 168, Short.MAX_VALUE)
                .add(addButton)
                .addContainerGap())
        );

        RentalTP.addTab("Tilføj", addRentalPanel);

        findDescLabel.setText("Her kan der søges på forskellige kriterier til udlån");

        findRentalLabel.setText("Udlejningsdato:");

        findExpectedDateLabel.setText("Forventet afl. dato:");

        findReturnedDateLabel.setText("Afleveringsdato:");

        findCustomerIdLabel.setText("Kunde:");

        findMachineIdLabel.setText("Maskine:");

        findDepositLabel.setText("Depositum:");

        findPricePerDayLabel.setText("Pris per dag:");

        findTotalPriceLabel.setText("Total pris:");

        findCustomerTF.setEnabled(false);

        findMachineTF.setEnabled(false);

        findSearchButton.setText("Søg");
        findSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findSearchButtonActionPerformed(evt);
            }
        });

        findCustomerFindButton.setText("Find");
        findCustomerFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomerFindButtonActionPerformed(evt);
            }
        });

        findMachineFindButton.setText("Find");
        findMachineFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findMachineFindButtonActionPerformed(evt);
            }
        });

        findScrollPane.setViewportView(findTable);

        findShowAllButton.setText("Vis alle");
        findShowAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findShowAllButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout findRentalPanelLayout = new org.jdesktop.layout.GroupLayout(findRentalPanel);
        findRentalPanel.setLayout(findRentalPanelLayout);
        findRentalPanelLayout.setHorizontalGroup(
            findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(findScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                    .add(findDescLabel)
                    .add(findRentalLabel)
                    .add(findRentalPanelLayout.createSequentialGroup()
                        .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(findExpectedDateLabel)
                            .add(findReturnedDateLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(findReturnedDateTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .add(findRentalTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .add(findExpectedDateTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(findDepositLabel)
                            .add(findPricePerDayLabel)
                            .add(findTotalPriceLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(findPricePerDaySP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                            .add(findTotalPriceSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, findDepositSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)))
                    .add(findRentalPanelLayout.createSequentialGroup()
                        .add(findSearchButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(findShowAllButton))
                    .add(findRentalPanelLayout.createSequentialGroup()
                        .add(findCustomerIdLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findCustomerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 176, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findCustomerFindButton))
                    .add(findRentalPanelLayout.createSequentialGroup()
                        .add(findMachineIdLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findMachineTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findMachineFindButton)))
                .addContainerGap())
        );
        findRentalPanelLayout.setVerticalGroup(
            findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findDescLabel)
                .add(18, 18, 18)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findRentalLabel)
                    .add(findDepositLabel)
                    .add(findDepositSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findRentalTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findExpectedDateLabel)
                    .add(findExpectedDateTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findPricePerDayLabel)
                    .add(findPricePerDaySP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findReturnedDateLabel)
                    .add(findReturnedDateTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findTotalPriceLabel)
                    .add(findTotalPriceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findCustomerIdLabel)
                    .add(findCustomerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findCustomerFindButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findMachineIdLabel)
                    .add(findMachineTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findMachineFindButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findSearchButton)
                    .add(findShowAllButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                .addContainerGap())
        );

        RentalTP.addTab("Søg", findRentalPanel);

        getDescLabel.setText("Her kan der søges på ID");

        getIdLabel.setText("ID:");

        getSearchButton.setText("Hent");
        getSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getSearchButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout getRentalPanelLayout = new org.jdesktop.layout.GroupLayout(getRentalPanel);
        getRentalPanel.setLayout(getRentalPanelLayout);
        getRentalPanelLayout.setHorizontalGroup(
            getRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(getDescLabel)
                    .add(getRentalPanelLayout.createSequentialGroup()
                        .add(getIdLabel)
                        .add(18, 18, 18)
                        .add(getIdSp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(getSearchButton))
                .addContainerGap(264, Short.MAX_VALUE))
        );
        getRentalPanelLayout.setVerticalGroup(
            getRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getRentalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getDescLabel)
                .add(18, 18, 18)
                .add(getRentalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(getIdLabel)
                    .add(getIdSp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(getSearchButton)
                .addContainerGap(290, Short.MAX_VALUE))
        );

        RentalTP.addTab("Hent", getRentalPanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(RentalTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 456, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(RentalTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 454, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addCustomerFindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomerFindButtonActionPerformed
		cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
}//GEN-LAST:event_addCustomerFindButtonActionPerformed

    private void findCustomerFindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCustomerFindButtonActionPerformed
        cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
}//GEN-LAST:event_findCustomerFindButtonActionPerformed

	private void addMachineFindButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addMachineFindButtonActionPerformed
	{//GEN-HEADEREND:event_addMachineFindButtonActionPerformed
		mp = new MachinePicker();
		mp.addWindowListener(this);
		mp.setVisible(true);
	}//GEN-LAST:event_addMachineFindButtonActionPerformed

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addButtonActionPerformed
	{//GEN-HEADEREND:event_addButtonActionPerformed
		if (this.chosenMachine != null && this.chosenCustomer != null && !addDepositSp.getValue().equals(0) && !addPricePerDaySp.getValue().equals(0))
		{
			this.controller.addRental(this.chosenMachine, this.chosenCustomer, Double.parseDouble(addDepositSp.getValue().toString()), Double.parseDouble(addPricePerDaySp.getValue().toString()));
			JOptionPane.showMessageDialog(this, addMachineTF.getText()+" er blevet lejet ud til "+addCustomerTF.getText()+".", "Udlejning oprettet", JOptionPane.INFORMATION_MESSAGE);
			this.chosenMachine = null;
			this.chosenCustomer = null;
			addMachineTF.setText("");
			addCustomerTF.setText("");
			findMachineTF.setText("");
			findCustomerTF.setText("");
			addDepositSp.setValue(0);
			addPricePerDaySp.setValue(0);
		}
		else
		{
			JOptionPane.showMessageDialog(this, "En eller flere informationer mangler.", "Manglende indtastning", JOptionPane.WARNING_MESSAGE);
		}
	}//GEN-LAST:event_addButtonActionPerformed

	private void findMachineFindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findMachineFindButtonActionPerformed
		mp = new MachinePicker();
		mp.addWindowListener(this);
		mp.setVisible(true);
	}//GEN-LAST:event_findMachineFindButtonActionPerformed

	private void getSearchButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_getSearchButtonActionPerformed
	{//GEN-HEADEREND:event_getSearchButtonActionPerformed
		Rental rental = this.controller.getRental(Integer.parseInt(getIdSp.getValue().toString()));
		if (rental != null) {
			new RentalInfoView(rental).setVisible(true);
		} else {
			JOptionPane.showMessageDialog(this, "Der er angivet et forkert ID.", "Forkert ID", JOptionPane.ERROR_MESSAGE);
		}
	}//GEN-LAST:event_getSearchButtonActionPerformed

	private void findSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findSearchButtonActionPerformed
		Date rentalDate = null, expectedReturnDate = null, returnDate = null;
		try
		{
			if(!findRentalTF.getText().equals(""))
			{
				rentalDate = Tools.makeDateFromString(findRentalTF.getText());
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Udlejningsdatoen er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
		}
		try
		{
			if(!findExpectedDateTF.getText().equals(""))
			{
				expectedReturnDate = Tools.makeDateFromString(findExpectedDateTF.getText());
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Den forventede afleveringsdato er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
		}
		try
		{
			if(!findReturnedDateTF.getText().equals(""))
			{
				returnDate = Tools.makeDateFromString(findReturnedDateTF.getText());
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Afleveringsdatoen er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
		}

		int deposit, pricePerDay, totalPrice;
		deposit = Integer.parseInt(findDepositSP.getValue().toString());
		pricePerDay = Integer.parseInt(findPricePerDaySP.getValue().toString());
		totalPrice = Integer.parseInt(findTotalPriceSP.getValue().toString());

		JOptionPane.showMessageDialog(this, findTable.getRowCount());
		ArrayList<Rental> findRental = this.controller.findRental(rentalDate, expectedReturnDate, returnDate, this.chosenMachine, this.chosenCustomer, deposit, pricePerDay, totalPrice);
		findTable.setModel(
			new RentalTableModel(
				findRental));
		JOptionPane.showMessageDialog(this, findTable.getRowCount());
	}//GEN-LAST:event_findSearchButtonActionPerformed

	private void findShowAllButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findShowAllButtonActionPerformed
	{//GEN-HEADEREND:event_findShowAllButtonActionPerformed
		findTable.setModel(new RentalTableModel(this.controller.getAllRentals()));
	}//GEN-LAST:event_findShowAllButtonActionPerformed

	public void windowClosing(WindowEvent e)
	{
		if(mp != null)
		{
			this.chosenMachine = mp.getChosenMachine();
			if(this.chosenMachine != null)
			{
				addMachineTF.setText(this.chosenMachine.getName());
				findMachineTF.setText(this.chosenMachine.getName());
			}
			mp = null;
		}
		else if(cp != null)
		{
			this.chosenCustomer = cp.getChosenCustomer();
			if(this.chosenCustomer != null)
			{
				addCustomerTF.setText(byggemarked.Tools.getNameOrCompanyName(chosenCustomer));
				findCustomerTF.setText(byggemarked.Tools.getNameOrCompanyName(chosenCustomer));
			}
			cp = null;
		}
	}

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane RentalTP;
    private javax.swing.JButton addButton;
    private javax.swing.JButton addCustomerFindButton;
    private javax.swing.JLabel addCustomerLabel;
    private javax.swing.JTextField addCustomerTF;
    private javax.swing.JLabel addDepositLabel;
    private javax.swing.JSpinner addDepositSp;
    private javax.swing.JLabel addDescLabel;
    private javax.swing.JButton addMachineFindButton;
    private javax.swing.JLabel addMachineLabel;
    private javax.swing.JTextField addMachineTF;
    private javax.swing.JSpinner addPricePerDaySp;
    private javax.swing.JLabel addPrisPerDayLabel;
    private javax.swing.JPanel addRentalPanel;
    private javax.swing.JButton findCustomerFindButton;
    private javax.swing.JLabel findCustomerIdLabel;
    private javax.swing.JTextField findCustomerTF;
    private javax.swing.JLabel findDepositLabel;
    private javax.swing.JSpinner findDepositSP;
    private javax.swing.JLabel findDescLabel;
    private javax.swing.JLabel findExpectedDateLabel;
    private javax.swing.JTextField findExpectedDateTF;
    private javax.swing.JButton findMachineFindButton;
    private javax.swing.JLabel findMachineIdLabel;
    private javax.swing.JTextField findMachineTF;
    private javax.swing.JLabel findPricePerDayLabel;
    private javax.swing.JSpinner findPricePerDaySP;
    private javax.swing.JLabel findRentalLabel;
    private javax.swing.JPanel findRentalPanel;
    private javax.swing.JTextField findRentalTF;
    private javax.swing.JLabel findReturnedDateLabel;
    private javax.swing.JTextField findReturnedDateTF;
    private javax.swing.JScrollPane findScrollPane;
    private javax.swing.JButton findSearchButton;
    private javax.swing.JButton findShowAllButton;
    private javax.swing.JTable findTable;
    private javax.swing.JLabel findTotalPriceLabel;
    private javax.swing.JSpinner findTotalPriceSP;
    private javax.swing.JLabel getDescLabel;
    private javax.swing.JLabel getIdLabel;
    private javax.swing.JSpinner getIdSp;
    private javax.swing.JPanel getRentalPanel;
    private javax.swing.JButton getSearchButton;
    // End of variables declaration//GEN-END:variables
}